package com.demo.Controller;

import com.alibaba.fastjson.JSONObject;

import com.demo.Pojo.Books;
import com.demo.service.FeignBookService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 测试服务熔断
 */

//@RestController
public class Con_Controller {

    /**
     * 为了单独测试降级需要将此进行注释，若是测试熔断解开

    @Resource
    public FeignBookService service;

    @RequestMapping("/getAll")
    public JSONObject getAll(){
        return service.queryAll();
    }

    @HystrixCommand(fallbackMethod = "queryBookById2")//如果这个有异常，就会调用备用服务
    @RequestMapping("/getById/{id}")
    public Books queryBookById(@PathVariable("id") int id){
        Books books = service.queryById(id);
        if (null == books){
            throw  new RuntimeException("没找到id对应的book");
        }
        return books;
    }

    //Hystrix 备选方案
    public Books queryBookById2(@PathVariable("id") int id){
        Books  books = new Books();
        books.setBookName("调用备用方案Hystrix：queryBookById2");
        return books;
    }


     */
}
